package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.pojo.DtsAd;
import com.qf.pojo.DtsAdmin;
import com.qf.service.AdminService;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.Date;

/**
 * @author lixu
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private DtsAdminMapper dtsAdminMapper;
    /**
     * 分页查询
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param username
     * @return
     */
    @Override
    public IPage<DtsAdmin> adminPage(Integer page, Integer limit, String sort, String order, String username) {
        //                           ?page=1&limit=20&sort=add_time&order=desc
        return dtsAdminMapper.selectPage(new Page<DtsAdmin>(page,limit),new QueryWrapper<DtsAdmin>()
                           .eq(StringUtils.isEmpty(username)?false:true,"username",username)
                           .orderBy(StringUtils.isEmpty(sort)?false:true,
                                   "asc".equalsIgnoreCase(order)?true:false,sort)

        );
    }

    /**
     * 修改
     * @param dtsAdmin
     */
    @Override
    public void update(DtsAdmin dtsAdmin) {
        dtsAdmin.setPassword(new BCryptPasswordEncoder().encode(dtsAdmin.getPassword()));
        dtsAdmin.setUpdateTime(new Date());
        dtsAdminMapper.updateById(dtsAdmin);
    }


}
